﻿using System.Collections.Generic;
using MedicalLog.Domain.Entities;
using System.Linq;

namespace MedicalLog.Domain.Repositories
{
    public class InsuranceRepository
    {
        MedicalLogEntities _dbMedicalLog;

        public InsuranceRepository()
        {
            _dbMedicalLog = new MedicalLogEntities();
        }

        /// <summary>
        /// This method adds an insurance to the database
        /// </summary>
        /// <param name="newInsurance"></param>
        public void AddInsurance(Insurance newInsurance)
        {
            _dbMedicalLog.AddToInsurances(newInsurance);
            _dbMedicalLog.SaveChanges();
        }

        /// <summary>
        /// This method delete an insurance from the database
        /// </summary>
        /// <param name="pId"></param>
        public void DeleteInsurance(int pId)
        {
            Insurance insurance = _dbMedicalLog.Insurances.Where(x => x.IdInsurance == pId).First();
            if (insurance != null)
            {
                _dbMedicalLog.Insurances.DeleteObject(insurance);
                _dbMedicalLog.SaveChanges();
            }
        }

        /// <summary>
        /// THis method display the detail of an Insurance
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public Insurance DetailInsurance(int pId)
        {
            Insurance insurance = _dbMedicalLog.Insurances.First(x => x.IdInsurance == pId);
            return insurance;
        }

        /// <summary>
        /// This methods updates an insurance
        /// </summary>
        /// <param name="oldInsurance"></param>
        public void UpdateInsurance(Insurance oldInsurance)
        {
            _dbMedicalLog.Insurances.FirstOrDefault(x => x.IdInsurance == oldInsurance.IdInsurance);
            _dbMedicalLog.Insurances.ApplyCurrentValues(oldInsurance);
            _dbMedicalLog.SaveChanges();
        }

        /// <summary>
        /// This method search an insurance from the database
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public Insurance SearchInsurance(int pId)
        {
            Insurance insurance = _dbMedicalLog.Insurances.First(x => x.IdInsurance == pId);
            return insurance;
        }

        /// <summary>
        /// This method return all of the insurances in the database.
        /// </summary>
        /// <returns></returns>
        public List<Insurance> GetInsurances()
        {
            return _dbMedicalLog.Insurances.ToList();
        }

        //public string GetInsuranceCompanyById(int id)
        //{
        //    var insuranceCompany = (from i in _dbMedicalLog.Insurances
        //                            where id == i.IdInsurance
        //                            select i.InsuranceCompany);
        //    return insuranceCompany.First().ToString();
        //}
    }
}
